package com.huiyujiaoyu.util;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

/**
 * Created by yujunlong on 2017/7/31.
 */

public class AnimatorUtil {
    /**
     * @param view 左侧筛选 下拉框
     *             关闭动画
     */
    public static  void animateClose(Context c, final LinearLayout view) {
        ValueAnimator animator = createAnimator(view, 0, DisplayUtil.dip2px(c, -150));

        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }
        });
        animator.start();
    }


    /**
     * @param view  左侧的筛选框
     * @param start 开始时 距离左侧的margin
     * @param end   结束时 距离左侧的margin
     * @return
     */
    public static ValueAnimator createAnimator(final LinearLayout view, int start, int end) {
        ValueAnimator animator = ValueAnimator.ofInt(start, end);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueanimation) {

                int value = (int) valueanimation.getAnimatedValue();
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) view.getLayoutParams();
                params.setMargins(value, 0, 0, 0);
                view.setLayoutParams(params);

            }
        });

        return animator;
    }

    /**
     * @param view 左侧筛选的 下拉框
     *             弹出动画
     */
    public static void animateOpen(Context c,LinearLayout view) {
        view.setVisibility(View.VISIBLE);
        ValueAnimator animator = createAnimator(view, DisplayUtil.dip2px(c, -150), DisplayUtil.dip2px(c, 0));
        animator.start();
    }
}
